﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GiaPhuc.Data;
using DbLinq.MySql;
using System.Data;
using GiaPhuc.Model;

namespace GiaPhuc
{
    public partial class _Default : System.Web.UI.Page
    {
        private string Keyword { get { return Request["keyword"] ?? String.Empty; } }
        private int CategoryID { get { return Convert.ToInt32(Request["CategoryID"]); } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadProducts();
            }
        }

        private void LoadProducts()
        {
            IList<ProductModel> products = null;
            using (IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Helper.ConnectionString))
            {
                using (ResellEraDMInPhUkIeNipAdX context = new ResellEraDMInPhUkIeNipAdX(connection))
                {
                    var queryProducts = from x in context.Products.ToList()
                                        join y in context.ProductsDescription
                                        on x.ProductsID equals y.ProductsID
                                        select new { x, y };
                    if (null != queryProducts && queryProducts.Count() > 0)
                    {
                        if (String.Empty != Keyword)
                        {
                            queryProducts = queryProducts.Where(z => z.y.ProductsName.ToLower().Contains(Keyword.ToLower()) || z.x.ProductsPrice.ToString().Contains(Keyword));
                        }
                        else if (0 != CategoryID)
                        {
                            queryProducts = queryProducts.Where(z => z.x.MasterCategoriesID == CategoryID);
                        }

                        products = new List<ProductModel>();
                        foreach (var item in queryProducts)
                        {
                            products.Add(new ProductModel
                            {
                                ProductID = item.x.ProductsID,
                                ProductName = item.y.ProductsName,
                                Price = item.x.ProductsPrice,
                                Specification = item.y.ProductsDescription1,
                                ImagePath = item.x.ProductsImage,
                                Url = ("/Pages/ProductDetail.aspx?ID=" + item.x.ProductsID).ContextualURL(item.y.ProductsName),
                                CartUrl = ("/Pages/CartPage.aspx?ProductID=" + item.x.ProductsID).ContextualURL(item.y.ProductsName),
                                CategoryID = item.x.MasterCategoriesID,
                                ClassNew = Convert.ToBoolean(item.x.ProductIsFree) ? "new" : String.Empty,
                                ClassHot = Convert.ToBoolean(item.x.ProductIsAlwaysFreeShipping) ? "hot" : String.Empty
                            });
                        }

                        this.datDefault.DataSource = products.OrderBy(x => x.CategoryID).ThenBy(x => x.Price);
                        this.datDefault.DataBind();

                        this.rptNewProducts.DataSource = products.Where(x => x.ClassNew == "new");
                        this.rptNewProducts.DataBind();

                        //this.rptSpecialProducts.DataSource = products.Where(x => x.ClassHot == "hot");
                        //this.rptSpecialProducts.DataBind();
                    }
                }
            }
        }

    }
}